<?php
/**
 * @Author:      zk
 * @DateTime:    18-4-9 下午5:30
 * @Description: cms 耗材分类   [同步 datamodel下的 DataSupplyClassification]
 */
class SupplyClassification extends Eloquent
{
    protected $table = "supply_classification";


    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-03T15:28:18+0800
     * @param    [type]                   $st_classifi_id     [description]
     * @return   [type]                                       [description]
     */
    public static function getClassification($st_classifi_id){
    	if(Cache::has('supply_classification_'.$st_classifi_id)){//如果有缓存的 标准耗材
            return Cache::get('supply_classification_'.$st_classifi_id);
        }else{
	        $si_arr= SupplyClassification::leftJoin("supply_classification as sc2","sc2.id","=","supply_classification.parent_id")
	        ->leftJoin("supply_classification as sc","sc.id","=","sc2.parent_id")
	        ->where('supply_classification.id',$st_classifi_id)
	        ->select("sc2.id as two_id","sc2.name as two","sc.id as one_id","sc.name as one","sc.is_sale as one_is_sale")->first();
	        if($si_arr){
                Cache::add('supply_classification_'.$st_classifi_id,$si_arr, 10);
            }
	        return $si_arr;
	    }
    
    }
    
    /**
     * @Author: zk
     * @Date:   18-6-5 下午5:00
     * @Description:    根据三级分类获取耗材的三级分类id数组
     * @param int $level_1      一级分类
     * @param int $level_2      二级分类    [可以为空]
     * @param int $level_3      三级分类    [可以为空]
     * @return array 
     */
    public static function getSupplyClassificationId($level_1, $level_2='', $level_3='') {
        $level_arr = [];    //耗材三级分类id数组
        if($level_1 != '' || $level_2 != '' || $level_3 != ''){
            $level = SupplyClassification::select('supply_classification.id', 'supply_classification.name')
                    ->leftJoin('supply_classification AS lev2', 'supply_classification.parent_id', '=', 'lev2.id')
                    ->leftJoin('supply_classification AS lev1', 'lev2.parent_id', '=', 'lev1.id');
            if($level_3 != ''){
                $level->where('supply_classification.level', 3);
                $level->where('supply_classification.id', $level_3);
            }
            if($level_2 != ''){
                $level->where('lev2.level', 2);
                $level->where('lev2.id', $level_2);
            }
            if($level_1 != ''){
                $level->where('lev1.level', 1);
                $level->where('lev1.id', $level_1);
            }
            $level = $level->orderBy('supply_classification.id')->get();
            if(!empty($level)){
                foreach ($level as $v) {
                    $level_arr[] = $v->id;
                }
            }
        }
        
        return $level_arr;
    }
    
}